package com.alan.repository.impl;

import com.alan.entity.admin;
import com.alan.entity.teach;
import com.alan.entity.teacherstudent;
import com.alan.repository.AdminRepository;
import com.alan.utils.JDBCTools;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AdminRepsitoryImpl implements AdminRepository {
    @Override
    public admin login(String username, String password) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        admin admin = null;
        try {
            connection = JDBCTools.getConnection();
            String sql = "select * from admin where name=? and password=?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, username);
            preparedStatement.setString(2, password);
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                admin = new admin(resultSet.getString(1), resultSet.getString(2));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCTools.release(connection, preparedStatement, resultSet);
        }
        return admin;
    }

    @Override
    public List<teacherstudent> findall(int index, int limit) {
        Connection connection = JDBCTools.getConnection();
        String sql = "select * from student limit ?,?";
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<teacherstudent> list = new ArrayList<>();
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, index);
            preparedStatement.setInt(2, limit);
            resultSet = preparedStatement.executeQuery();
            while ((resultSet.next())) {
                teacherstudent teacherstudent = new teacherstudent(resultSet.getString(1), resultSet.getString(2), resultSet.getString(3), resultSet.getString(4), resultSet.getString(5), resultSet.getInt(6), resultSet.getDouble(7), resultSet.getDouble(8), resultSet.getString(9));
                list.add(teacherstudent);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCTools.release(connection, preparedStatement, resultSet);
        }
        return list;
    }


    @Override
    public List<teach> findallteacher(int index, int limit) {
        Connection connection = JDBCTools.getConnection();
        String sql = "select * from teacher limit ?,?";
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<teach> list = new ArrayList<>();
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, index);
            preparedStatement.setInt(2, limit);
            resultSet = preparedStatement.executeQuery();
            while ((resultSet.next())) {
                teach teach = new teach(resultSet.getString(1), resultSet.getString(2), resultSet.getString(3));
                list.add(teach);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCTools.release(connection, preparedStatement, resultSet);
        }
        return list;
    }

    @Override
    public int counts() {
        Connection connection = JDBCTools.getConnection();
        String sql = "select count(*) from student";
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        int count = 0;
        try {
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            while ((resultSet.next())) {
                count = resultSet.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCTools.release(connection, preparedStatement, resultSet);
        }
        return count;
    }

    @Override
    public int teachercounts() {
        Connection connection = JDBCTools.getConnection();
        String sql = "select count(*) from teacher";
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        int count = 0;
        try {
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            while ((resultSet.next())) {
                count = resultSet.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCTools.release(connection, preparedStatement, resultSet);
        }
        return count;
    }

    @Override
    public List<teach> findteacher() {
        Connection connection = JDBCTools.getConnection();
        String sql = "select * from teacher";
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<teach> list = new ArrayList<>();
        int count = 0;
        try {
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            while ((resultSet.next())) {
                list.add(new teach(resultSet.getString(1), resultSet.getString(2), resultSet.getString(3)));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCTools.release(connection, preparedStatement, resultSet);
        }
        return list;
    }

    @Override
    public void update(String name, String password, String t_class) {
        Connection connection = JDBCTools.getConnection();
        String sql = "update teacher set password=?,t_class=? where name=?";
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<teach> list = new ArrayList<>();
        int count = 0;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, password);
            preparedStatement.setString(2, t_class);
            preparedStatement.setString(3, name);
            int a = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCTools.release(connection, preparedStatement, resultSet);
        }
    }

    @Override
    public void addstudent(String stu_id, String Cname, String Ename, String tel, String stu_class, int semester, double mmarks, double emarks, String status) {
        Connection connection = JDBCTools.getConnection();
        String sql = "insert into student values (?,?,?,?,?,?,?,?,?)";
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, stu_id);
            preparedStatement.setString(2, Cname);
            preparedStatement.setString(3, Ename);
            preparedStatement.setString(4, tel);
            preparedStatement.setString(5, stu_class);
            preparedStatement.setInt(6, semester);
            preparedStatement.setDouble(7, mmarks);
            preparedStatement.setDouble(8, emarks);
            preparedStatement.setString(9, status);
            int a = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCTools.release(connection, preparedStatement, resultSet);
        }
    }

    @Override
    public void delete(String name, String t_class) {
        Connection connection = JDBCTools.getConnection();
        String sql = "delete from teacher where name=? and t_class=?";
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, name);
            preparedStatement.setString(2, t_class);
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCTools.release(connection, preparedStatement, resultSet);
        }

    }

    @Override
    public void change(String Nname, String Npassword, String name, String password) {
        Connection connection = JDBCTools.getConnection();
        String sql = "update admin set name=?,password=? where name=? and password=?";
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, Nname);
            preparedStatement.setString(2, Npassword);
            preparedStatement.setString(3, name);
            preparedStatement.setString(4, password);
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCTools.release(connection, preparedStatement, resultSet);
        }
    }

    @Override
    public admin find(String username) {
        Connection connection = JDBCTools.getConnection();
        String sql = "select * from admin where name=?";
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        admin admin=null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, username);
            resultSet=preparedStatement.executeQuery();
            if(resultSet.next()){
                admin = new admin(resultSet.getString(1), resultSet.getString(2));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCTools.release(connection, preparedStatement, resultSet);
        }
        return admin;
    }

    @Override
    public void newpassword(String name, String password) {
        Connection connection = JDBCTools.getConnection();
        String sql = "update admin set password=? where name=?";
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, password);
            preparedStatement.setString(2, name);
            int a = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCTools.release(connection, preparedStatement, resultSet);
        }
    }


}
